
import React, { Component } from 'react';
import { observer, inject } from 'mobx-react';
import { withRouter } from 'react-router-dom';
// import { Button } from 'weaver-mobile';
import { SearchAdvanced,ListAdvanced } from 'weaver-mobile-page';
import { Tools } from 'weaver-mobile';
const getLabel = Tools.getLabel;
const store =[
  'searchWorkplanStore',
  'viewWorkplanStore'
]
@inject(...store)

@withRouter
@observer
export default class Search extends Component {
  componentDidMount() {
    window.em && window.em.checkJsApi('changeTitle') && window.em.changeTitle({ title: getLabel(18482,'查询日程') })
    this.onSearch();
  }

  componentWillReceiveProps(nextProps){
    let prePathName = this.props.location.pathname
    let curPathName = nextProps.location.pathname
    if(curPathName&&curPathName!==prePathName){
      if(curPathName == "/calendar/search"){
        window.em && window.em.checkJsApi('changeTitle') && window.em.changeTitle({ title: getLabel(18482,'查询日程') })
      }
    }
  }
  componentWillUnmount() {
    // 卸载组件
    const { listAd } = this.props.searchWorkplanStore;
    const {  listViewStore,setState } = listAd;
    // 清除列表数据
    setState({searchParams: {}}); //清空searchParams
    listViewStore.clearData();
    // localStorage.removeItem('routeType');
  }

  onSearch = () => {
    const { listAd,dataFetchOptions } = this.props.searchWorkplanStore;
    const { searchAdvancedStore, listViewStore, searchParams } = listAd;

    // 搜索框等搜索数据
    let params = { ...searchParams, };
    // 获取高级搜索参数
    if (searchAdvancedStore) {
      const { formParams } = searchAdvancedStore.form;
      params = { ...formParams,};
    }
    // 清除列表数据
    listViewStore.clearData();
    // 带参数请求列表数据
    params = {...params,ismobile: 1}
    listAd.getData({...dataFetchOptions, params});
  }

  onRowClick = (e) => {
    if(window.em) {
      window.em.openLink({
        url:`/spa/workplan/static4mobile/index.html#/detail?workplanId=${e.ID}`, // 要打开链接的地址
        openType: 2, // 打开方式 1:当前窗口 2:新窗口 3:系统默认浏览器
        sysId: window.localStorage.emobile_ec_id, // 集成系统ID
        corpId:'', // 企业corpid
        agentId:'', // 应用id
        success: function (res) {
        }
      });
    } else {
      let {changeWorkplanID} = this.props.viewWorkplanStore;
      changeWorkplanID(e.ID);
      this.props.history.push(`/detail?workplanId=${e.ID}`)
    }
  }

  render() {
    const { searchWorkplanStore } = this.props;
    const { search,listAd,dataFetchOptions,conditionFetchOptions } = searchWorkplanStore;

    return (
      <div>
        <ListAdvanced
          store={listAd}
          hasAdvanceSerach
          rowKey="ID"
          searchKey="planname"
          dataFetchOptions={dataFetchOptions}
          conditionFetchOptions={conditionFetchOptions}
          showCheck={false}
          buttonGroupData={[{
            key: 'submit',
            text: getLabel(386788,'确定'),
            onClick: this.onSearch,
            type: 'primary',
          }]}
          onSearch={() => this.onSearch()}
          onRowClick={(e) => {this.onRowClick(e)}}
        />
      </div>
    );
  }
}